package com.cloud.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloud.entity.ConvertTaskEntity;
import com.cloud.entity.PointEntity;
import com.cloud.entity.ResultEntity;

import java.util.List;

/**
 * @author Henry.Song
 * @version 2020/12/21
 * @description
 */
public class EntityUtil {

    /** CoordPoints Convert **/
    /**
     * convertCoordPointsForDB
     * @param entity
     */
    public static void convertCoordPointsForDB(JSONObject entity) {
        Object coordPoints = entity.get("coordPoints");
        if (coordPoints != null) {
            String str = JsonUtil.toString(coordPoints);
            entity.put("coordPoints", str);
        }
    }

    public static void convertCoordPoints(ConvertTaskEntity entity) {
        Object coordPoints = entity.get("coordPoints");
        if (coordPoints != null) {
            List<PointEntity> pointEntityList = entity.getCoordPoints();
            entity.setCoordPoints(pointEntityList);
        }
    }

    public static void convertCoordPoints(JSONObject jsonObject) {
        if (jsonObject.get("coordPoints") != null) {
            Object coordPoints = jsonObject.getJSONArray("coordPoints");
            jsonObject.put("coordPoints", coordPoints);
        }
    }

    /** AlgorithmInfo Convert **/
    /**
     * convertAlgorithmInfoForDB
     * @param entity
     */
    public static void convertAlgorithmInfoForDB(ConvertTaskEntity entity) {
        Object algorithmInfo = entity.get("algorithmInfo");
        if (algorithmInfo != null) {
            String str = JsonUtil.toString(algorithmInfo);
            entity.put("algorithmInfo", str);
        }
    }

    public static void convertAlgorithmInfo(ConvertTaskEntity entity) {
        Object algorithmInfo = entity.get("algorithmInfo");
        if (algorithmInfo != null) {
            Object object = entity.getAlgorithmInfo();
            entity.setAlgorithmInfo(object);
        }
    }

    public static void convertAlgorithmInfo(JSONObject jsonObject) {
        if (jsonObject.get("algorithmInfo") != null) {
            Object algorihtmInfo = jsonObject.getJSONObject("algorithmInfo");
            jsonObject.put("algorithmInfo", algorihtmInfo);
        }
    }

    /** SkipCoordPoints **/
    /**
     *
     * @param entity
     */
    public static void convertSkipCoordPointsForDB(JSONObject entity) {
        Object coordPoints = entity.get("skipCoordPoints");
        if (coordPoints != null) {
            String str = JsonUtil.toString(coordPoints);
            entity.put("skipCoordPoints", str);
        }
    }

    public static void convertSkipCoordPoints(ConvertTaskEntity entity) {
        Object coordPoints = entity.get("skipCoordPoints");
        if (coordPoints != null) {
            List<PointEntity> pointEntityList = entity.getSkipCoordPoints();
            entity.setSkipCoordPoints(pointEntityList);
        }
    }

    public static void convertSkipCoordPoints(JSONObject jsonObject) {
        if (jsonObject.get("skipCoordPoints") != null) {
            Object coordPoints = jsonObject.getJSONArray("skipCoordPoints");
            jsonObject.put("skipCoordPoints", coordPoints);
        }
    }

    public static void formatConvertTaskEntityForDB(ConvertTaskEntity entity) {
        convertCoordPointsForDB(entity);
        convertSkipCoordPointsForDB(entity);
        convertAlgorithmInfoForDB(entity);
    }

    public static void formatConvertTaskEntity(ConvertTaskEntity entity) {
        convertCoordPoints(entity);
        convertSkipCoordPoints(entity);
        convertAlgorithmInfo(entity);
    }

    public static void formatConvertTaskJSONObject(JSONObject jsonObject) {
        convertCoordPoints(jsonObject);
        convertSkipCoordPoints(jsonObject);
        convertAlgorithmInfo(jsonObject);
    }

  public static ResultEntity getAlgorihtmResultSample() {
    ResultEntity entity = new ResultEntity();
    entity.setCode(100);
    entity.setInfo(null);
    entity.setMsg("请求成功");
    //entity.put("taskFile", null);
    //entity.put("taskType", "1");
    //entity.put("algorithmType", "1");
    entity.put("taskName", "name_b029");
    entity.put("id", 12);
    //entity.put("taskStatus", "8");
    //entity.put("coordPoints", JSON.parseArray("[{'x':32,'y':43},{'x':64,'y':50}]"));
    entity.put("info", JSON.parseObject("{'zone':'xyyhf', 'count':32}"));
    return entity;
  }
}
